package com.arthur.controller;

import com.arthur.bean.Product;
import com.arthur.bean.User;
import com.arthur.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @所在项目:maven_ssm202008_wanghongzhao
 * @所在包:com.arthur.controller
 * @作者:arthur_wang
 * @创建时间:2020-08-28 19:08:23
 * @说明:登录的控制器
 **/
@RestController
public class LoginController {
    Logger logger = LogManager.getLogger(LoginController.class);
    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @RequestMapping("/LoginController")
    @ResponseBody
    public Map login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> objectMap = new HashMap<>();
        String result = null;
        request.setCharacterEncoding("UTF-8");

        String   username = request.getParameter("username");
        String   userpwd = request.getParameter("userpwd");

        if (username == null || username.equals("")) {
            result = "-1";
        } else if (userpwd == null || userpwd.equals("")) {
            result = "-2";
        } else {
            int rows = userService.loginByUsernameAndUserpwd(username, userpwd);
            if (rows == 1) {
                logger.info(username + "登陆成功");
                request.getSession().setAttribute("login_name", username);
                result = "1";
            } else {
                logger.info(username + "登陆失败");
                result = "0";
            }
        }
        objectMap.put("result", result);
        return objectMap;
    }

    @RequestMapping("/GetLoginInfoController")
    @ResponseBody
    public Map getLoginInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String,Object> objectMap=new HashMap<>();
        String login_name= (String)request.getSession().getAttribute("login_name");

        if (login_name!=null&&!login_name.equals("")){
            objectMap.put("result",login_name);
            User temp_userinfo = userService.fetchUserinfoByName(login_name);
            if(temp_userinfo!=null){
                objectMap.put("userinfo",temp_userinfo);
                objectMap.put("userinfoR",1);
            }else{
                objectMap.put("userinfoR",0);
            }
        }else{
            objectMap.put("result","0");
        }
        return objectMap;
    }
    @RequestMapping("/LogoutController")
    @ResponseBody
    public Map logoutController(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Map<String,Object> objectMap=new HashMap<>();
        request.getSession().removeAttribute("login_name");
        objectMap.put("result","1");
        return objectMap;
    }
}
